//
//  MyWishesViewController.m
//  Beauty
//
//  Created by Snow on 16/9/20.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MyWishesViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "WishesListCollectionViewCell.h"
#import "ModelOfWishes.h"
#import "NewProductViewController.h"
@interface MyWishesViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrayOfWishes;

@end

@implementation MyWishesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
}
- (void)createView
{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumLineSpacing = 5;
    _layout.minimumInteritemSpacing = 5;
    _layout.itemSize = CGSizeMake((self.view.bounds.size.width - 20) / 3, 120);
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[WishesListCollectionViewCell class] forCellWithReuseIdentifier:@"WishesListCollectionViewCell2"];
    [self createFresh];
}

- (void)createFresh
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
}
- (void)handleLoadNewData {
    //请求服务器更新数据
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=favorites_list&key=%@&page=100", password];

    _arrayOfWishes = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"favorites_list"];
        for (NSDictionary *dict in arr) {
            ModelOfWishes *model = [[ModelOfWishes alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfWishes addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
    [self.collectionView.mj_header endRefreshing];
}

- (void)handleData
{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=favorites_list&key=%@&page=100", password];
    _arrayOfWishes = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"favorites_list"];
        for (NSDictionary *dict in arr) {
            ModelOfWishes *model = [[ModelOfWishes alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfWishes addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfWishes.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WishesListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WishesListCollectionViewCell2" forIndexPath:indexPath];
    ModelOfWishes *model = [_arrayOfWishes objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.goods_image_url]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewProductViewController *vc = [[NewProductViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    ModelOfWishes *model = [_arrayOfWishes objectAtIndex:indexPath.row];
    vc.goods_id = model.goods_id;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
